AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")


function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("atm_sent") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 1)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it 
		
		return ent -- You need to return the entity to make it work
end

function ENT:Initialize()
	util.AddNetworkString("openATMmenu")
	util.AddNetworkString("withdraw")
	util.AddNetworkString("deposit")
	util.AddNetworkString("balanceUpdate")

	self:SetModel("models/props/de_nuke/equipment1.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)

	local phys = self:GetPhysicsObject()
	self.nodupe = true
	self.ShareGravgun = true

	phys:Wake()
end


function ENT:Use(usr,caller)
	usr:GetTable().bankBalance = usr:GetTable().bankBalance or 500
	loadPlayerInfo(usr)
	net.Start("openATMmenu")
		net.WriteString(tostring(usr:GetTable().bankBalance))
	net.Send(usr)
end

function Withdraw(ply,amount)
	if tonumber(ply:GetTable().bankBalance) < tonumber(amount) then
		GAMEMODE:Notify(ply, 1, 4, "Insufficient funds")
	elseif amount>0 then
		ply:AddMoney(amount)
		//ply.DarkRPVars.money = ply.DarkRPVars.money + amount
		ply:GetTable().bankBalance = ply:GetTable().bankBalance - amount
		GAMEMODE:Notify(ply, 2, 4, "Withdrew $"..amount.." from your account.  Your balance is now $"..ply:GetTable().bankBalance)
		net.Start("balanceUpdate")
			net.WriteString(tostring(ply:GetTable().bankBalance))
		net.Send(ply)
		savePlayerInfo(ply)
	end
end
net.Receive("withdraw", function(length,client) Withdraw(client,tonumber(net.ReadString())) end)
	

function Deposit(ply,amount)
	if tonumber(ply.DarkRPVars.money) < amount then
		GAMEMODE:Notify(ply, 1, 4, "Insufficient funds")
	elseif amount>0 then
		ply:AddMoney(-amount)
		ply:GetTable().bankBalance = ply:GetTable().bankBalance + amount
		GAMEMODE:Notify(ply, 2, 4, "Deposited $"..amount.." in your account.  Your balance is now $"..ply:GetTable().bankBalance)
		net.Start("balanceUpdate")
			net.WriteString(ply:GetTable().bankBalance)
		net.Send(ply)
		savePlayerInfo(ply)
	end
end
net.Receive("deposit", function(length,client) Deposit(client,tonumber(net.ReadString())) end)